
import * as THREE from 'three'
import { Tooltip } from './tooltip'
import { baseCircleMaterial, roundTableMaterialArry, showCircle } from '../../materal'
export class IOT {
  constructor(camera, itemData) {
    this.name = itemData.name
    this.data = itemData
    this.camera = camera
    this.instance = new THREE.Object3D()
    this.instance.name = itemData.name
    this.tween = null
    this.setPosition(itemData.position)
    this.circle2 = null
    this.draw()
    this.addTooltip(itemData)
  }
  setPosition(position) {
    this.instance.position.y = position.y
    this.instance.position.x = position.x
    this.instance.position.z = position.z
  }
  async draw(data) {
    // 底座
    const geometry = new THREE.CircleBufferGeometry(3, 32)
    const material = baseCircleMaterial
    this.circle = new THREE.Mesh(geometry, material)
    this.circle.name = this.name
    this.circle.rotation.x = -Math.PI / 2
    this.circle.position.y = 3
    // this.circle.visible = false
    this.instance.add(this.circle)
    // 圆台

    const geometry3 = new THREE.CylinderBufferGeometry(1.5, 1.5, 0.5, 32)
    const cylinder3 = new THREE.Mesh(geometry3, roundTableMaterialArry)
    // cylinder3.rotation.x = -Math.PI / 2
    cylinder3.position.y = 3.5
    this.instance.add(cylinder3)
    // 展示牌的圆
    const circle2texture = await showCircle(this.data.type)
    circle2texture.minFilter = THREE.LinearFilter
    const showCircleMaterial = new THREE.MeshBasicMaterial({
      side: THREE.DoubleSide,
      map: circle2texture,
      transparent: true
    })
    const geometry2 = new THREE.CircleBufferGeometry(2, 32)
    this.circle2 = new THREE.Mesh(geometry2, showCircleMaterial)
    this.circle2.position.y = 6
    this.circle2.lookAt(this.camera.position)
    this.instance.add(this.circle2)
    this.animate()
  }
  hide() {
    this.instance.visible = false
  }
  show() {
    this.instance.visible = true
  }
  // 添加信息弹窗
  addTooltip(data) {
    // 把名字传入data
    this.tooltip = new Tooltip()
    this.instance.add(this.tooltip.instance)
    const conf = {
      position: {
        x: -10,
        y: 30,
        z: 30
      },
      scale: {
        x: 0.15,
        y: 0.13,
        z: 1
      }
    }
    this.tooltip.draw(data.data, data.name, conf)
    this.unhandleSelected()
  }
  // 选中时
  handleSelected() {
    this.tooltip.show()
    this.circle.visible = true
    this.StartAnimate()
    // this.changeMeshMaterial(0)
  }
  // 未选中时
  unhandleSelected() {
    this.tooltip.hide()
    this.circle.visible = false
    if (this.tooltip) {
      this.StopAnimate()
    }

    // this.changeMeshMaterial(1)
  }
  animate() {

  }
  StartAnimate() {

  }
  StopAnimate() {
    this.circle2.position.y = 6
    this.circle.scale.y = 1
    this.circle.scale.x = 1
  }
}
